using Domain;
using Domain.Services;
using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Permissions;
using System;
using System.Web.UI.WebControls;
using UserInterface.Misc;

namespace UserInterface
{
    public partial class EditTopic : BoardPageBase
    {
        private Topic topic;
        private ForumPermissionManager forumPermissionManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            int topicId = Convert.ToInt32(Request.QueryString["topicId"]);
            string pageTitle = (string)GetLocalResourceObject("Title");

            topic = TopicService.GetById(topicId);
            forumPermissionManager = PermissionManager.CreateForumPermissionManager(topic.Forum);
            if (CurrentSession.IsGuest || (topic.Locked && !forumPermissionManager.Moderate) || (!forumPermissionManager.Moderate && (!forumPermissionManager.EditOwn || topic.StartMessage.Poster.Id != CurrentSession.UserId)))
            {
                DieMessage(DieMessageType.InsufficientPermissions, "InsufficientPermissions");
            }

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.TitleGenerator.ItemName = topic.Subject;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(topic.Forum.Name, "ViewForum.aspx?forumId=" + topic.Forum.Id);
            MasterPage.AddPathItem(topic.Subject, "ViewTopic.aspx?topicId=" + topic.Id);
            MasterPage.AddPathItem(pageTitle, "EditTopic.aspx?topicId=" + topic.Id);

            if (!Page.IsPostBack)
            {
                TopicTypeRadioButtonList.Items.Add(new ListItem("Normal", Convert.ToString((byte)TopicType.Normal)));
                if (forumPermissionManager.CreateSticky || topic.Type == TopicType.Sticky)
                {
                    TopicTypeRadioButtonList.Items.Add(new ListItem("Sticky", Convert.ToString((byte)TopicType.Sticky)));
                }
                if (forumPermissionManager.CreateAnnouncement || topic.Type == TopicType.Announcement)
                {
                    TopicTypeRadioButtonList.Items.Add(new ListItem("Announcement", Convert.ToString((byte)TopicType.Announcement)));
                }
                if (forumPermissionManager.CreateFaq || topic.Type == TopicType.Faq)
                {
                    TopicTypeRadioButtonList.Items.Add(new ListItem("Faq", Convert.ToString((byte)TopicType.Faq)));
                }

                SubjectTextBox.Text = topic.Subject;
                Editor.Text = topic.StartMessage.Text;
                TopicTypeRadioButtonList.SelectedValue = Convert.ToString((byte)topic.Type);

                PreviewMessage.Visible = false;
            }
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            string subject = SubjectTextBox.Text.Trim();
            string text = Editor.Text.Trim();

            if (subject != "" && text != "")
            {
                TopicType type = (TopicType)Convert.ToByte(TopicTypeRadioButtonList.SelectedValue);
                TopicService.UpdateTopic(topic, subject, text, type);

                Response.Redirect("ViewTopic.aspx?topicId=" + topic.Id);
            }
            else
            {
                MasterPage.Messages.AddCritical(Resources.Resource.InvalidFormData);
            }
        }

        protected void PreviewButton_Click(object sender, EventArgs e)
        {
            PreviewMessage.Visible = true;
            Message message = topic.StartMessage;
            PreviewMessage.Message = message;
        }
    }
}
